package bao;

import DBCon.DBCon;
import model.Book;
import model.Customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomerDao {
        //   创建 注册 dao层方法
         DBCon db = new DBCon();
         Connection con;
         PreparedStatement pstm;
         ResultSet rs ;
         List<Customer> customers = new ArrayList<>();
         int flag;
         //  用户注册
         public boolean InsertCustomer(Customer customer){
         try{
             con = db.dbCon();
             String sql = "insert into customer (u_id,u_name,u_password,u_email,u_tel,u_adress) values(?,?,?,?,?,?)";
             pstm = con.prepareStatement(sql);
             pstm.setInt(1,customer.getU_id());
             pstm.setString(2,customer.getU_name());
             pstm.setString(3,customer.getU_password());
             pstm.setString(4,customer.getU_emali());
             pstm.setString(5,customer.getU_tel());
             pstm.setString(6,customer.getAddres());
             flag = pstm.executeUpdate();
             if(flag>0){
                 return true;
             }
             return false;
    } catch(Exception e){
             e.printStackTrace();
    }
         return false;
         }
         //  用户登录
         public boolean CheckLogin(Customer customer){
             con = db.dbCon();
             String sql = "select * from customer where u_name=? and u_password=?";
             try{
                 pstm = con.prepareStatement(sql);
                 pstm.setString(1,customer.getU_name());
                 pstm.setString(2,customer.getU_password());
                 rs =  pstm.executeQuery();
                 if(rs.next()){
                     customer.setU_name(rs.getString("u_name"));
                     return true;
                 }else{

                 }
             } catch(Exception e){
                 e.printStackTrace();
             }
             return false;
         }
         // 查询用户信息
         public List<Customer> QueryCustomerInformation() {
             // 连接数据库成功

             con = db.dbCon();
             String sql = "select * from customer";
             try {
                 pstm = con.prepareStatement(sql);
                 rs = pstm.executeQuery();
                 while (rs.next()) {
                     // 结果集对象获取数据库个字段的对象
                     int id = rs.getInt("u_id");
                     String name = rs.getString("u_name");
                     String password = rs.getString("u_password");
                     String  sex = rs.getString("u_sex");
                     String email = rs.getString("u_email");
                     String tel = rs.getString("u_tel");
                     String adress = rs.getString("u_adress");
                     // 创建customer对象  将各数据字段对象调用构造方法存入customer对象
                     Customer customer = new Customer(id,name,password,sex,email,tel,adress);
                     customers.add(customer);
                 }
                 return customers;
             } catch (SQLException e) {
                 e.printStackTrace();
             } finally {
                 db.close();
             }
             return null;
         }
}
